﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;

namespace Warehouse.Web.Inventory
{
    public partial class Initialization : BasePage
    {
        private List<DataWrapper> datalist = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlWarehouse.DataSource = DataAccess.Warehouse.GetList();
                this.ddlWarehouse.DataBind();

                this.btnSave.Visible = false;
            }
        }

        void LoadData()
        {
            int houseId = int.Parse(this.ddlWarehouse.SelectedValue);

            var inventories = DataAccess.InventoryInitialization.GetList(p => p.HouseID == houseId);

            var products = DataAccess.ProductSpecificationView.GetList();
            datalist = new List<DataWrapper>();
            foreach (var sn in products.GroupBy(p => p.SerialNumber))
            {
                bool firstproduct = true;
                foreach (var product in sn.GroupBy(p => p.ProductID))
                {
                    bool firstSpec = true;
                    foreach (var spec in product)
                    {
                        DataWrapper d = new DataWrapper()
                        {
                            FirstProduct = firstproduct,
                            FirstSpecification = firstSpec,
                            Product = spec,
                            ProductSpecificatoinCount = sn.Count(),
                            SpecificatoinCount = product.Count(),
                            Inventory = 0
                        };
                        var inventory = inventories.SingleOrDefault(p => p.ProductSpecificationID == spec.ID);
                        if (inventory != null)
                        {
                            d.Inventory = inventory.Count;
                        }
                        datalist.Add(d);

                        if (firstSpec)
                        {
                            firstSpec = false;
                        }
                        if (firstproduct)
                        {
                            firstproduct = false;
                        }
                    }
                }
            }

            this.rptCategories.DataSource = DataAccess.ProductCategory.GetList();
            this.rptCategories.DataBind();
            this.btnSave.Visible = true;
        }

        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int houseId = int.Parse(this.ddlWarehouse.SelectedValue);
            var spects = DataAccess.ProductSpecification.GetList();
            var inventories = DataAccess.InventoryInitialization.GetList(p => p.HouseID == houseId);
            foreach (var s in spects)
            {
                var data = inventories.SingleOrDefault(p => p.ProductSpecificationID == s.ID);
                int count = 0;
                if (Request.Form.AllKeys.Contains("Inventory_" + s.ID))
                {
                    count = int.Parse(Request.Form["Inventory_" + s.ID]);
                    if (count < 0)
                    {
                        RegisterClientMessage("库存不能为负");
                        return;
                    }
                }
                if (data == null && count == 0)
                {
                    continue;
                }
                if (data == null)
                {
                    data = new Warehouse.DataAccess.InventoryInitialization()
                    {
                        HouseID = houseId,
                        ProductSpecificationID = s.ID,
                    };
                }
                data.Count = count;
                data.Save();
            }

            RegisterClientMessage("保存成功");
            LoadData();
        }

        protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item == null)
                return;
            DataAccess.ProductCategory category = e.Item.DataItem as DataAccess.ProductCategory;
            Repeater rptProducts = e.Item.FindControl("rptProducts") as Repeater;
            if (category != null && rptProducts != null)
            {
                rptProducts.DataSource = datalist.Where(p => p.Product.CategoryID == category.ID);
                rptProducts.DataBind();
            }

        }


        class DataWrapper
        {
            public bool FirstProduct { get; set; }
            public bool FirstSpecification { get; set; }
            public int ProductSpecificatoinCount { get; set; }
            public int SpecificatoinCount { get; set; }
            public DataAccess.ProductSpecificationView Product { get; set; }
            public int Inventory { get; set; }
        }
    }
}
